package dao;

import application.exceptions.ItemNotFoundException;
import mappings.Country;
import mappings.Material;
import mappings.Signature;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import utils.HibernateUtil;

import java.util.List;

public class SignatureDao
{
    private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public List<Signature> getSignaturesForMaterial(int materialId)
    {
        Session session = sessionFactory.openSession();

        List<Signature> signatures = session.createCriteria(Signature.class)
                .add(Restrictions.eq("material.id", materialId))
                .list();

        session.close();

        return signatures;
    }

    public Signature addSignature(int materialId, int countryId, String name) throws ItemNotFoundException
    {
        Signature signature = new Signature();

        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Material material = (Material) session.get(Material.class, materialId);
        Country country = (Country) session.get(Country.class, countryId);

        if(material == null || country == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        signature.setMaterial(material);
        signature.setCountry(country);
        signature.setName(name);

        session.save(signature);

        tx.commit();
        session.close();

        return signature;
    }

    public Signature editSignature(Integer id, int materialId, int countryId, String name) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Country country = (Country) session.get(Country.class, id);
        Material material = (Material) session.get(Material.class, materialId);
        Signature signatureToEdit = (Signature) session.get(Signature.class, id);

        if(signatureToEdit == null || material == null || country == null)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        signatureToEdit.setMaterial(material);
        signatureToEdit.setCountry(country);
        signatureToEdit.setName(name);
        session.update(signatureToEdit);
        tx.commit();
        session.close();

        return signatureToEdit;
    }

    public void deleteSignature(Integer id) throws ItemNotFoundException
    {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        int affectedRows = session.createQuery("delete Signature where id = :id")
                .setParameter("id", id)
                .executeUpdate();

        if(affectedRows == 0)
        {
            tx.rollback();
            session.close();
            throw new ItemNotFoundException();
        }

        tx.commit();
        session.close();
    }
}
